import 'package:provider/provider.dart';
import 'package:flutter/material.dart';
import 'package:shop/config/index.dart';
import 'package:shop/pages/category_page.dart';
import 'package:shop/provide/current_index_provide.dart';
import 'home_page.dart';
import 'cart_page.dart';
import 'member_page.dart';

class IndexPage extends StatelessWidget {
  final List<BottomNavigationBarItem> bottomTabs = [
    const BottomNavigationBarItem(
      icon: Icon(Icons.home),
      label: KString.homeTitle, // 首页
    ),
    const BottomNavigationBarItem(
      icon: Icon(Icons.category),
      label: KString.categoryTitle, // 分类
    ),
    const BottomNavigationBarItem(
      icon: Icon(Icons.shopping_cart),
      label: KString.shoppingCartTitle, // 购物车
    ),
    const BottomNavigationBarItem(
      icon: Icon(Icons.person),
      label: KString.memberTitle, // 个人中心
    ),
  ];

  final List<Widget> tabBodies = [
    const HomePage(),
    const CategoryPage(),
    const CartPage(),
    const MemberPage()
  ];

  IndexPage({super.key});

  @override
  Widget build(BuildContext context){
    return Consumer<CurrentIndexProvide>(
      builder: (context,child,val){
        int currentIndex = Provider.of<CurrentIndexProvide>(context).currentIndex;

        return Scaffold(
          backgroundColor: const Color.fromRGBO(244, 245, 245, 1.0),
          bottomNavigationBar: BottomNavigationBar(
            items: bottomTabs,
            type: BottomNavigationBarType.fixed,
            currentIndex: currentIndex,
            onTap: (index){
              print(index);
              Provider.of<CurrentIndexProvide>(context, listen: false).changeIndex(index);
            }
          ),
          body: IndexedStack(
            index: currentIndex,
            children: tabBodies,
          )
        );
      },
    );
  }
}